package com.lzq.badminton.quartz;

import com.lzq.badminton.entity.Booking;
import com.lzq.badminton.service.ArenaService;
import com.lzq.badminton.service.BookingService;
import com.lzq.badminton.service.CourtTemplateService;
import com.lzq.badminton.utils.BeanCopyUtils;
import com.lzq.badminton.vo.AddBookingCourtTemplateVo;
import com.lzq.badminton.vo.ArenaVo;
import com.lzq.badminton.vo.CourtTemplateVo;
import com.lzq.badminton.vo.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class AddBookingJob implements Job {

    @Autowired
    private BookingService bookingService;

    @Autowired
    private CourtTemplateService courtTemplateService;

    @Autowired
    private ArenaService arenaService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.debug("定时器启动!!!!!!!!!!!!!!!!!!!");
        //遍历所有的球馆
        List<ArenaVo> arenaVos = arenaService.listArena();
        LocalDate now = LocalDate.now();

        for(ArenaVo arena : arenaVos){
            now = LocalDate.now();
            Long arenaId = arena.getId();
            //得到场地计划表
            List<CourtTemplateVo> courtTemplateVos = courtTemplateService.listCourtTemplate(arenaId);
            List<AddBookingCourtTemplateVo> addBookingCourtTemplateVos = BeanCopyUtils.copyBeanList(courtTemplateVos, AddBookingCourtTemplateVo.class);
            List<Booking> bookingList = BeanCopyUtils.copyBeanList(addBookingCourtTemplateVos, Booking.class);
            for(int i = 0; i < arena.getAdDate(); i++) {
                //将当前日期赋值给bookingList
                LocalDate finalNow = now.plusDays(i);
                bookingList.stream()
                        .map(booking -> {
                            booking.setDate(finalNow);
                            return booking;
                        }).collect(Collectors.toList());
                bookingService.addBookings(bookingList);
            }
        }

    }
}
